﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class OnllyPage1UIController : MonoBehaviour {

    public static OnllyPage1UIController Instance;
    private int[,] arr = new int[13, 3];//tf_middle的存储
    private int i;                      //行（0开始）
    private int j;                      //列（0开始）
    public Transform tf_page1;
    private bool b_isEnterClick;
    public string s_temp;
    public float f_temp;
    public Text t_dianliu;           //电流变化文本框
    public Transform tf_right;       //动作值，返回值，返回系数的父集
    public bool b_isDongzuo;         //手动试验下是否“动作”了
    public Transform tf_shijianjilu; //手控事件记录
    public Text t_data;              //手控事件记录的时间
    public Transform tf_result;      //结果

    private float f_time;            //手控试验开始后指示灯多久闪一次的时间
    public float f_timeShow;         //动作之后多久显示返回值
    private float tempTime = 0.0f;   //返回时间
    private bool b_isGetFanhui;      //是否获取到了返回值
    // Use this for initialization
    void Start () {
        Instance = this;
        EventManager<string>.AddEvent(MyEventType.OnllyDirButtonClickEvent,OnDirButtonClick);
        EventManager<string>.AddEvent(MyEventType.XuanzhuanAnniuEvent,OnXuanzhuanAnniu);
        EventManager<bool>.AddEvent(MyEventType.OnllyReSetPageEvent,ReSet);
        EventManager<bool>.AddEvent(MyEventType.OnllyEnterButtonClickEvent,OnEnterClick);
        EventManager<string>.AddEvent(MyEventType.OnllyNumberButtonClickEvent,numController);
        Init();
    }

    void Update() {
        if (OnllyPage2UIController._instance.b_shoukongStart) {
            f_time += Time.deltaTime;
            if (f_time >= 0.8f) {
                OnllyUIController._instance.ChangeZhishideng(true, 0.5f);
                f_time = 0;
            }
            if (b_isDongzuo && !b_isEnterClick && !b_isGetFanhui) {//动作之后且不是输入框的状态
                f_timeShow += Time.deltaTime;
                if (f_timeShow > 2.5f)
                    getfanhui(tempTime);
            }   
        }
    }

    /// <summary>
    /// 手控试验
    /// </summary>
    public void shoukongshiyan()
    {
        if (OnllyPage2UIController._instance.testType == "手控" && OnllyPage2UIController._instance.b_shoukongStart && !b_isDongzuo) {
            float temp1 = float.Parse(ShijianJiluUIController._instance.PinjieDianliu(11));        //设置的I段动作电流
            float temp2 = float.Parse(ShijianJiluUIController._instance.PinjieDianliu(13));        //设置的II段动作电流
            float temp3 = float.Parse(ShijianJiluUIController._instance.PinjieDianliu(15));        //设置的III段动作电流

            float[] temp = new float[3] { temp3, temp2, temp1 };
            Array.Sort(temp);
            float dianliu = float.Parse(t_dianliu.GetComponent<Text>().text);                      //获取当前电流的数值
            
            for (int i = 2; i >=0; i--)
            {
                if (dianliu >= temp[i])
                {
                        if (isDayuTen(dianliu))
                        {
                            tf_right.Find("dongzuozhi").GetComponent<Text>().text = dianliu.ToString("0.00") + "A";                        //onlly设备界面右下角的动作电流
                            tf_shijianjilu.Find("dianliu").GetComponent<Text>().text = dianliu.ToString("0.00") + "A    0.011A    0.002A"; //四方设备事件记录界面的Ia
                        }
                        else
                        {
                            tf_right.Find("dongzuozhi").GetComponent<Text>().text = dianliu.ToString("0.000") + "A";                        //onlly设备界面右下角的动作电流
                            tf_shijianjilu.Find("dianliu").GetComponent<Text>().text = dianliu.ToString("0.000") + "A    0.011A    0.002A"; //四方设备事件记录界面的Ia
                        }
                        if (temp1 == temp[i])
                        {
                            tempTime = float.Parse(ShijianJiluUIController._instance.PinjieDianliu(12)) + GetTime();
                            tf_shijianjilu.Find("time").GetComponent<Text>().text = tempTime.ToString("0.00"); //四方设备事件记录界面的动作时间
                            tf_shijianjilu.Find("time").GetComponent<Text>().text += "''过流I段动作";
                            Unit unit = new Unit(1, "dongzuo");
                            EventManager<Unit>.TriggerEvent(MyEventType.ZhishidengTypeEvent, unit);
                        }
                        else if (temp2 == temp[i])
                        {
                            tempTime = float.Parse(ShijianJiluUIController._instance.PinjieDianliu(14)) + GetTime();
                            tf_shijianjilu.Find("time").GetComponent<Text>().text = tempTime.ToString("0.00");
                            tf_shijianjilu.Find("time").GetComponent<Text>().text += "''过流II段动作";
                            Unit unit = new Unit(2, "dongzuo");
                            EventManager<Unit>.TriggerEvent(MyEventType.ZhishidengTypeEvent, unit);
                        }
                        else if (temp3 == temp[i])
                        {
                            tempTime = float.Parse(ShijianJiluUIController._instance.PinjieDianliu(16)) + GetTime();
                            tf_shijianjilu.Find("time").GetComponent<Text>().text = tempTime.ToString("0.00");
                            tf_shijianjilu.Find("time").GetComponent<Text>().text += "''过流III段动作";
                            Unit unit = new Unit(3, "dongzuo");
                            EventManager<Unit>.TriggerEvent(MyEventType.ZhishidengTypeEvent, unit);
                        }
                        t_data.text = System.DateTime.Now.ToString("MM - dd   HH : mm : fff");
                        b_isDongzuo = true;
                    b_isGetFanhui = false;
                    EventManager<bool>.TriggerEvent(MyEventType.ChangeUIToShijianjiluEvent, true);   //实验结束后“四方”设备界面跳转到事件记录界面
                    break; 
                }
            }
        }
    }

    /// <summary>
    /// 获取动作电流的误差
    /// </summary>
    /// <returns></returns>
    float GetTime() {
        System.Random ran = new System.Random();
        return ran.Next(2,20)/100.0f;
    }

    /// <summary>
    /// 写入结果界面的5条数据
    /// </summary>
    void Jieguo(float time) {
        System.Random ran = new System.Random();
        tf_result.Find("0").GetComponent<Text>().text = tf_right.Find("dongzuozhi").GetComponent<Text>().text;
        tf_result.Find("1").GetComponent<Text>().text = (time * 1000.0f).ToString("0.0") + "MS"; 
        tf_result.Find("2").GetComponent<Text>().text = tf_right.Find("fanhuizhi").GetComponent<Text>().text;
        tf_result.Find("3").GetComponent<Text>().text = ran.Next(2000, 3000).ToString("0.0") + "MS";
        tf_result.Find("4").GetComponent<Text>().text = tf_right.Find("fanhuixishu").GetComponent<Text>().text;
    }

    /// <summary>
    /// 获取返回值跟返回系数
    /// </summary>
    void getfanhui(float time) {
        if (b_isDongzuo) {
            TimerMngr.Instance.Create(() =>
            {
                b_isGetFanhui = true;
                tf_right.Find("fanhuizhi").GetComponent<Text>().text = t_dianliu.GetComponent<Text>().text+"A";
                string s_dongzuo = tf_right.Find("dongzuozhi").GetComponent<Text>().text;
                float dongzuo = float.Parse( s_dongzuo.Remove(s_dongzuo.Length-1) );
                string s_fanhui = tf_right.Find("fanhuizhi").GetComponent<Text>().text;
                float fanhui = float.Parse(s_fanhui.Remove(s_fanhui.Length - 1));
                float xishu = fanhui / dongzuo;
                tf_right.Find("fanhuixishu").GetComponent<Text>().text = xishu.ToString("f3");
                Jieguo(time);
            }).Start(0.5f);
        }
    }

    private void OnEnterClick(bool active)
    {
        if (OnllyUIController._instance.i_pageNum == 0 && !OnllyPage2UIController._instance.b_isStart && !OnllyUIController._instance.b_isDuihuakuang)
        {
            string temp = i + "_" + j;
            if (OnllyPage2UIController._instance.testType == "手控" && !b_isEnterClick && i == 6 && j == 1)
            {
                OnllyUIController._instance.ChangeZhishideng(false, 0.5f);
                tf_page1.Find(temp).Find("guangbiao").gameObject.SetActive(true);//光标出现
                OnllyUIController._instance.ChangeType(tf_page1, 0, temp);
                //数字居左
                tf_page1.Find(temp).GetChild(1).GetComponent<Text>().alignment = TextAnchor.MiddleLeft;
                //有数据的话清除原先的数据
                tf_page1.Find(temp).GetChild(1).GetComponent<Text>().text = "";
                //单位隐藏
                tf_page1.Find(temp).GetChild(2).gameObject.SetActive(false);
                //高亮隐藏
                tf_page1.Find(temp).GetChild(0).gameObject.SetActive(false);
                //数字键输入数字
                //Enter
                b_isEnterClick = true;
            }
            else if (b_isEnterClick) {
                OnllyUIController._instance.ChangeZhishideng(false, 0.5f);
                tf_page1.Find(temp).Find("guangbiao").gameObject.SetActive(false);//光标消失
                tf_page1.Find(temp).GetChild(2).GetComponent<Text>().color = Color.white;
                OnllyUIController._instance.ChangeType(tf_page1, 5, temp);
                b_isEnterClick = false;
                s_temp = tf_page1.Find(temp).GetChild(1).GetComponent<Text>().text;
                //单位显示
                tf_page1.Find(temp).GetChild(2).gameObject.SetActive(true);
                //高亮显示
                tf_page1.Find(temp).GetChild(0).gameObject.SetActive(true);
                if (s_temp == "") {
                    s_temp = "0.000";
                }
                f_temp = float.Parse(s_temp);
                if (isDayuTen(f_temp))
                {
                    tf_page1.Find(temp).GetChild(1).GetComponent<Text>().text = f_temp.ToString("0.00");
                }
                else
                    tf_page1.Find(temp).GetChild(1).GetComponent<Text>().text = f_temp.ToString("0.000");
                tf_page1.Find(temp).GetChild(1).GetComponent<Text>().alignment = TextAnchor.MiddleCenter;
                shoukongshiyan();
            }
        }
    }

    /// <summary>
    /// 输到一半的时候，按了方向键后，记录此时的输入值
    /// </summary>
    /// <param name="name"></param>
    void OnEnterTrue(string name) {
        if (OnllyPage2UIController._instance.testType == "手控" && i == 6 && j == 1 && b_isEnterClick) {
            tf_page1.Find(name).Find("guangbiao").gameObject.SetActive(false);//光标消失
            b_isEnterClick = false;
            tf_page1.Find(name).GetChild(2).gameObject.SetActive(true);
            s_temp = tf_page1.Find(name).GetChild(1).GetComponent<Text>().text;
            if (s_temp == "") {
                s_temp = "0.000";
            }
            f_temp = float.Parse(s_temp);
            if (isDayuTen(f_temp))
            {
                tf_page1.Find(name).GetChild(1).GetComponent<Text>().text = f_temp.ToString("0.00");
            }
            else
                tf_page1.Find(name).GetChild(1).GetComponent<Text>().text = f_temp.ToString("0.000");
            tf_page1.Find(name).GetChild(1).GetComponent<Text>().alignment = TextAnchor.MiddleCenter;
            OnllyUIController._instance.ChangeType(tf_page1, 1, name);
            tf_page1.Find(name).GetChild(2).GetComponent<Text>().color = new Color32(200, 38, 81, 255);
            shoukongshiyan();
        }
    }

    /// <summary>
    /// 判断得到的数字是否>=10
    /// </summary>
    /// <param name="num"></param>
    /// <returns></returns>
    private bool isDayuTen(float num)
    {
        if (num >= 10.0f)
        {
            return true;
        }
        return false;
    }

    private void OnDirButtonClick(string name)
    {
        if (OnllyUIController._instance.i_pageNum == 0 && !OnllyPage2UIController._instance.b_isStart && !OnllyUIController._instance.b_isDuihuakuang)
        {
            switch (name) {
                case "up":
                    OnUpButtonClick(true);break;
                case "down":
                    OnDownButtonClick(true);break;
                case "left":
                    OnLeftButtonClick(true);break;
                case "right":
                    OnRightButtonClick(true);break;
                default:break;
            }
        }
    }

    /// <summary>
    /// 数字键+增加，减少+“-”
    /// </summary>
    /// <param name="num"></param>
    private void numController(string num)
    {
        if (OnllyUIController._instance.i_pageNum == 0 && OnllyPage2UIController._instance.testType=="手控")
        {
            string name = 6 + "_" + 1;
            if (b_isEnterClick)
            {
                if (num == ".")
                {
                    if (!tf_page1.Find(name).GetChild(1).GetComponent<Text>().text.Contains("."))//只能有一个“.”
                    {
                        tf_page1.Find(name).GetChild(1).GetComponent<Text>().text += num;
                    }
                }
                else if (num == "-")//删除最后一个数字
                {
                    string temp = tf_page1.Find(name).GetChild(1).GetComponent<Text>().text;
                    if (temp.Length >= 1)
                    {
                        temp = temp.Substring(0, temp.Length - 1);
                        tf_page1.Find(name).GetChild(1).GetComponent<Text>().text = temp;
                        s_temp = tf_page1.Find(name).GetChild(1).GetComponent<Text>().text;
                    }
                }
                else if(num != "--")
                {
                    for (int temp = 0; temp <= 9; temp++)
                    {
                        if (int.Parse(num) == temp)
                        {
                            tf_page1.Find(name).GetChild(1).GetComponent<Text>().text += num;
                            s_temp = tf_page1.Find(name).GetChild(1).GetComponent<Text>().text;
                            break;
                        }
                    }
                }
            }
            if (!b_isEnterClick && num == "--")
            {
                s_temp = tf_page1.Find(name).GetChild(1).GetComponent<Text>().text;
                if (s_temp == "")
                    s_temp = "0";
                f_temp = float.Parse(s_temp);
                f_temp -= OnllyPage2UIController._instance.f_dianliu;
                if (f_temp <= 0)
                {
                    f_temp = 0.000f;
                }
                if (isDayuTen(f_temp))
                {
                    tf_page1.Find(name).GetChild(1).GetComponent<Text>().text = f_temp.ToString("0.00");
                }
                else
                    tf_page1.Find(name).GetChild(1).GetComponent<Text>().text = f_temp.ToString("0.000");
                shoukongshiyan();
            }
            //             else if (num == "++")
            //             {
            //                 s_temp = tf_page1.Find(name).GetChild(1).GetComponent<Text>().text;
            //                 f_temp = float.Parse(s_temp);
            //                 f_temp += OnllyPage2UIController._instance.f_dianliu;
            //                 if (isDayuTen(f_temp))
            //                 {
            //                     tf_page1.Find(name).GetChild(1).GetComponent<Text>().text = f_temp.ToString("0.00");
            //                 }
            //                 else
            //                     tf_page1.Find(name).GetChild(1).GetComponent<Text>().text = f_temp.ToString("0.000");
            //             }
        }
    }
    void Init() {
        for (int i = 0; i < 13; i++) {
            arr[i, 0] = 2;
        }
        for (int i = 3; i <= 5; i++)
        {
            for (int j = 1; j <= 2; j++)
            {
                arr[i, j] = 3;
            }
        }
        for (int i = 9; i <= 11; i++)
        {
            for (int j = 1; j <= 2; j++)
            {
                arr[i, j] = 3;
            }
        }
    }

    private void OnXuanzhuanAnniu(string name) {
        if (OnllyUIController._instance.i_pageNum == 0 && !OnllyPage2UIController._instance.b_isStart && !OnllyUIController._instance.b_isDuihuakuang) {
            switch (name) {
                case "shun":
                    OnRightButtonClick(true); break;
                case "ni":
                    OnDownButtonClick(true); break;
                default:break;
            }
        }
    }
    private void OnRightButtonClick(bool obj)
    {
        if (OnllyUIController._instance.i_pageNum==0) {
            string name = i + "_" + j;
            OnEnterTrue(name);
            if (i == 6 && j == 1)
            {
                OnllyUIController._instance.ChangeType(tf_page1, 1, name);
                tf_page1.Find(name).GetChild(2).GetComponent<Text>().color = new Color32(200, 38, 81, 255);
            }
            else if (arr[i, j] == 3) {
                OnllyUIController._instance.ChangeType(tf_page1, 3, name);
            }
            else
                OnllyUIController._instance.ChangeType(tf_page1, 0, name);
            j++;
            if (j == 3) {
                j = 0;
            }else
            if (i == 12 && j == 2) {
                j = 0;
            }
            name = i + "_" + j;
            if (arr[i, j] == 2)
            {
                OnllyUIController._instance.ChangeType(tf_page1, 4, name);
            }
            else
                OnllyUIController._instance.ChangeType(tf_page1, 5, name);
            if (i == 6 && j == 1)
            {
                tf_page1.Find(name).GetChild(2).GetComponent<Text>().color = Color.white;
            }
        }
    }

    private void OnLeftButtonClick(bool obj)
    {
        if (OnllyUIController._instance.i_pageNum == 0)
        {
            string name = i + "_" + j;
            OnEnterTrue(name);
            if (i == 6 && j == 1)
            {
                OnllyUIController._instance.ChangeType(tf_page1, 1, name);
                tf_page1.Find(name).GetChild(2).GetComponent<Text>().color = new Color32(200, 38, 81, 255);
            }
            else if (arr[i, j] == 3)
            {
                OnllyUIController._instance.ChangeType(tf_page1, 3, name);
            }
            else
                OnllyUIController._instance.ChangeType(tf_page1, 0, name);
            j--;
            if (j == -1)
            {
                j = 2;
            }else
            if (i == 12 && j == 2)
            {
                j = 1;
            }
            name = i + "_" + j;
            if (arr[i, j] == 2)
            {
                OnllyUIController._instance.ChangeType(tf_page1, 4, name);
            }
            else
                OnllyUIController._instance.ChangeType(tf_page1, 5, name);
            if (i == 6 && j == 1)
            {
                tf_page1.Find(name).GetChild(2).GetComponent<Text>().color = Color.white ;
            }
        }
    }

    private void OnUpButtonClick(bool obj)
    {
        if (OnllyUIController._instance.i_pageNum == 0)
        {
            string name = i + "_" + j;
            OnEnterTrue(name);
            if (i == 6 && j == 1)
            {
                OnllyUIController._instance.ChangeType(tf_page1, 1, name);
                tf_page1.Find(name).GetChild(2).GetComponent<Text>().color = new Color32(200, 38, 81, 255);
            }
            else if (arr[i, j] == 3)
            {
                OnllyUIController._instance.ChangeType(tf_page1, 3, name);
            }
            else
                OnllyUIController._instance.ChangeType(tf_page1, 0, name);
            i--;
            if (i == -1)
            {
                i = 12;
            }
            if (i == 12 && j == 2)
            {
                i = 11;
            }
            name = i + "_" + j;
            if (arr[i, j] == 2)
            {
                OnllyUIController._instance.ChangeType(tf_page1, 4, name);
            }
            else
                OnllyUIController._instance.ChangeType(tf_page1, 5, name);
            if (i == 6 && j == 1)
            {
                tf_page1.Find(name).GetChild(2).GetComponent<Text>().color = Color.white;
            }
        }
    }

    private void OnDownButtonClick(bool obj)
    {
        if (OnllyUIController._instance.i_pageNum == 0)
        {
            string name = i + "_" + j;
            OnEnterTrue(name);
            if (i == 6 && j == 1)
            {
                OnllyUIController._instance.ChangeType(tf_page1, 1, name);
                tf_page1.Find(name).GetChild(2).GetComponent<Text>().color = new Color32(200, 38, 81, 255);
            }
            else if (arr[i, j] == 3)
            {
                OnllyUIController._instance.ChangeType(tf_page1, 3, name);
            }
            else
                OnllyUIController._instance.ChangeType(tf_page1, 0, name);
            i++;
            if (i == 13)
            {
                i = 0;
            }else
            if (i == 12 && j == 2)
            {
                i = 0;
            }
            name = i + "_" + j;
            if (arr[i, j] == 2)
            {
                OnllyUIController._instance.ChangeType(tf_page1, 4, name);
            }
            else
                OnllyUIController._instance.ChangeType(tf_page1, 5, name);
            if (i == 6 && j == 1)
            {
                tf_page1.Find(name).GetChild(2).GetComponent<Text>().color = Color.white;
            }
        }
    }

    private void ReSet(bool obj) {
        if (OnllyUIController._instance.i_pageNum == 0) {
            string name = i + "_" + j;
            if (i == 6 && j == 1)
            {
                OnllyUIController._instance.ChangeType(tf_page1, 1, name);
                tf_page1.Find(name).GetChild(2).GetComponent<Text>().color = new Color32(200, 38, 81, 255);
            }
            else if (arr[i, j] == 3)
            {
                OnllyUIController._instance.ChangeType(tf_page1, 3, name);
            }
            else
                OnllyUIController._instance.ChangeType(tf_page1, 0, name);
            i = 0;
            j = 0;
            name = i + "_" + j;
            OnllyUIController._instance.ChangeType(tf_page1, 4, name);

        }     
    }
}
